package com.lipi.mall.member.service.impl;

import com.lipi.mall.member.exception.PhoneExistException;
import com.lipi.mall.member.exception.UserNameExistException;
import com.lipi.mall.member.service.MemberLevelService;
import com.lipi.mall.member.vo.MemberLoginVO;
import com.lipi.mall.member.vo.MemberRegistVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lipi.common.utils.PageUtils;
import com.lipi.common.utils.Query;

import com.lipi.mall.member.dao.MemberDao;
import com.lipi.mall.member.entity.MemberEntity;
import com.lipi.mall.member.service.MemberService;
import org.springframework.util.CollectionUtils;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {


    @Autowired
    private MemberLevelService memberLevelService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVO vo) {
        //检查用户名和手机号是否唯一
        checkMobileUnique(vo.getPhone());
        checkUsernameUnique(vo.getUserName());


        MemberEntity memberEntity = new MemberEntity();

        //设置默认信息
        //设置用户名
        memberEntity.setUsername(vo.getUserName());
        //设置昵称
        memberEntity.setNickname(vo.getUserName());
        //设置手机号
        memberEntity.setMobile(vo.getPhone());
        //设置默认等级
        Long defaultLevel = memberLevelService.getDefaultLevel();
        memberEntity.setLevelId(defaultLevel);


        //设置用户名
        memberEntity.setUsername(vo.getUserName());
        //设置密码，MD5+盐加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String recode = passwordEncoder.encode(vo.getPassword());

        memberEntity.setPassword(recode);


        this.baseMapper.insert(memberEntity);
    }

    @Override
    public void checkMobileUnique(String mobile) throws PhoneExistException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", mobile));
        if (count > 0) {
            throw new PhoneExistException();
        }
    }

    @Override
    public void checkUsernameUnique(String username) throws UserNameExistException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if (count > 0) {
            throw new UserNameExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVO vo) {
        MemberEntity entity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", vo.getLoginacct()).or().eq("mobile", vo.getLoginacct()));
        if (entity != null) {
            String dbPassword = entity.getPassword();

            //密码匹配
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean matches = passwordEncoder.matches(vo.getPassword(), dbPassword);
            if (matches) {
                return entity;
            }
        }
        return null;
    }
}